package kino.net;

import kino.client.KinoClient;

import java.util.ArrayList;
import java.util.List;

/**
 * 命令帧
 */
public class RedisFrame {

    /**
     * 参数个数
     */
    private int argsCount = 0;
    /**
     * 参数列表 其中 第0个参数为命令名称，如 set name zxz 其中第0个参数set为命令名称 后续的参数可能是子命令，可能是扩展参数，可能是值，由具体命令实现而定
     */
    private List<String> ArgsData = null;
    /**
     * 原始命令字节，用于aof文件存储
     */
    private byte[] oriQueryBytes;

    /**
     * 该帧从属的客户端
     */
    private KinoClient client;

    private int state = 0;
    public RedisFrame(int argsCount){
        this.argsCount = argsCount;
        this.ArgsData = new ArrayList<String>(argsCount);

    }
    public void AppendArgs(byte[] args){
        this.ArgsData.add(new String(args));
    }
    public int getCommandCount(){
        return ArgsData.size();
    }

    public String GetFirstCommand(){
        if (ArgsData.size() > 0){
            return ArgsData.get(0);
        }
        return null;
    }
    public String GetCommand(int index){
        if (ArgsData.size() > index){
            return ArgsData.get(index);
        }
        return null;
    }
    public byte[] getOriQueryBytes() {
        return oriQueryBytes;
    }

    public void setOriQueryBytes(byte[] oriQueryBytes) {
        this.oriQueryBytes = oriQueryBytes;
    }
    public String argsToInputLine(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0 ; i < ArgsData.size(); i++) {
            if (i != 0) {
                sb.append(" ");
            }
            sb.append(ArgsData.get(i));
        }
        return sb.toString();
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public KinoClient getClient() {
        return client;
    }

    public void setClient(KinoClient client) {
        this.client = client;
    }

}